//A simple extension for easier finding the personal best runs.
//By Carl Ribbegårdh
//http://www.gps-speedsurfing.com/default.aspx?mnu=user&smnu=sessions&uid=3165

console.log("GPS-Colorizer-Extension");

var coloredLinks = [];
var colors = ["#B37400", "#829900", "#B31B00", "#369900", "#0098B3", "#80006C", "#003699", "#8A5A00"];
//http://www.w3schools.com/css/css_colornames.asp (GPS-Speedsurf uses "Orange" = #FFA500 for normal active link color.)
//http://www.colorschemer.com/online.html


//---------------------------------------------------------
//-- Color the matching links when the PR box is visible --
//---------------------------------------------------------
$(document)
    //Find the box containing the records.
    .find("div[id$=divmyprs]")
    .find("tr.ColS_A_Blockcontent")
    //Locate all of the links in the relevant rows within the found box. (a in td in tr.cols...)
    .find("a")
    .each(function(index) {
    
        //Read the link url.
        var href = $(this).attr("href");
        //Find the questionmark and keep what's remaining after it.
        var argStart = href.indexOf("?");
        if (-1 < argStart) {
            href = href.substr(argStart + 1);
        }
        
        //If the url is not already colorized. (One color per session.)
        if (-1 == $.inArray(href, coloredLinks)) {
        
            //Save it for the remaining links, so we can compare it in the row above.
            coloredLinks.push(href);
            
            //Retrieve the color from the avaliable colors.
            var color = colors[$.inArray(href, coloredLinks)];

            console.log("Coloring ", $(this).text(), " using ", color);
            
            //Go through all links in all of the document,
            //and if they are pointing to the same session as the current link
            //we'll give it the chosen color.
            $("a[href*='" + href + "']").css("color", color);

            //Found: "/?mnu=user&val=61556&uid=3165"
            //Ex: "default.aspx?mnu=user&val=62083&uid=3165"
        }
    });

//-----------------------------------------------------------------------------
//-- Highlight the matching PR in the box below, when showing a user session --
//-----------------------------------------------------------------------------

//If the page is a user page.
var urlIdx = document.URL.indexOf("mnu=user")
if (-1 != urlIdx) {

    //Pick the interesting part of the url.
    var href = document.URL.substr(urlIdx);

    //Go through all links in all of the document,
    //and give the matching ones the color Orange (seems like a good fit).
    $("a[href*='" + href + "']").css("color", "#8A5A00");
    
}